// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";

const { ccclass, property } = cc._decorator;

export enum MovieClipEvent {
    PLAYOVER_EVENT = "playOver_event"
}

@ccclass
export default class MovieClipUI extends cc.Component {

    @property(sp.Skeleton)
    anim: sp.Skeleton = null;


    animNames: any[] = [{ anim1: "001", anim2: "001R" }, { anim1: "002", anim2: "002R" }, { anim1: "003", anim2: "003R" },
    { anim1: "004", anim2: "004R" }, { anim1: "005", anim2: "005R" }];
    currentId: number;
    currentAnimName: string;
    protected onLoad(): void {
        this.currentId = 0;
        let that = this;
        this.anim.setCompleteListener((e) => {
            that.currentAnimName = e.animation.name;

            if (e.animation.name == that.animNames[that.currentId].anim1) {
                that.anim.setAnimation(0, that.animNames[that.currentId].anim2, true);
            }
        });
        this.anim.setAnimation(0, this.animNames[this.currentId].anim1, false);
    }

    protected onEnable(): void {

    }

    protected onDisable(): void {

    }
    playOver() {
        this.node.active = false;
        SaveManager.single.userdata.isPlayMovie = true;
        SaveManager.single.saveData();

        GameEvent.eventNode.emit(MovieClipEvent.PLAYOVER_EVENT);
    }

    onClick() {
        if (this.currentId >= this.animNames.length - 1) {
            this.playOver();
            return;
        }
        if (this.currentAnimName == this.animNames[this.currentId].anim2) {
            this.currentAnimName = "";
            this.currentId++;
            this.anim.setAnimation(0, this.animNames[this.currentId].anim1, false);
        }
    }
}
